/*
 * option.cpp
 *
 *  Created on: Sep 26, 2009
 *      Author: karthik
 *
 *		Write a program to price Calendar Call spreads.
 *		The first type of data set will consist of values of the portfolio on a discrete grid of time-spot values for a fixed volatility.
 *		The
 *
 *
 *
 *
 *
 */
#include<iostream>
#include<math.h>
#include "Option.h"
#include "normal.h"
#include "Date.h"

Option::Option() {

}

Option::~Option() {

}

/*
 * Currently, the code calculates price only for call options.
 * With little modification, we can make it work for put options also.
 *
 */
double Option::call_price(double spot, double rate, double volatility, Date current_date){

	double call_price,d1,d2,z;

	double tau = time_diff_yrs(expiration,current_date);
	if (tau<=0){
		if (this->isCall){

			if (spot>strike){
				return spot-strike;
			}
			else{
				return 0;
			}
			//		tau=0;
		}
		else{
			if (spot<strike){
				return strike-spot;
			}
			else{
				return 0;
			}
		}
	}
	using namespace std;
	//	cout<<"pricing:"<<endl;
	//	cout<<tau<<endl;
	z = volatility*sqrt(tau);
	d1 = ( log(spot/strike) + (rate+(pow(volatility,2)/2))*(tau) )/z;
	//	cout<<d1<<endl;
	d2 = d1 - z;
	//	cout<<d2<<endl;

	call_price = (spot * nc(d1)) - (strike * exp(-1*rate*tau) * nc(d2));

	//	std::cout<<"current option price : "<<call_price<<std::endl;

	return call_price;

}

void Option::initialize(std::string symbol, Date exp, double K, bool CallFlag=true ){
	expiration=exp;
	strike=K;
//	rate=r;
	isCall = CallFlag;
	this->symbol = symbol;
}
